package com.bosssoft.g4.controller;

import com.bosssoft.common.result.Result;
import com.bosssoft.common.util.MyBeanUtil;
import com.bosssoft.g4.api.ResourceApi;
import com.bosssoft.g4.bo.SubSystemTree;
import com.bosssoft.g4.dto.ResourceDTO;
import com.bosssoft.g4.service.ResourceService;
import com.bosssoft.g4.vo.ResourceVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Baiye959
 */
@RestController
@RequestMapping("/api/v1/permission")
@Slf4j
@Api(tags = "资源管理相关接口")
public class ResourceController implements ResourceApi {

    @Autowired
    private ResourceService resourceService;

    @Override
    public Result<List<ResourceVO>> getResourceList(ResourceDTO resourceDTO) {
        log.info("获取资源列表");
        List<ResourceDTO> resourceDTOList = resourceService.getResourceList(resourceDTO);

        // 使用ResourceVO::new作为Supplier来生成ResourceVO实例
        List<ResourceVO> resourceVOList = MyBeanUtil.copyList(resourceDTOList, ResourceVO::new, null);

        return Result.success(resourceVOList);
    }

    @Override
    public Result<ResourceVO> getResourceById(@RequestParam("id") @NotNull Long id) {
        log.info("获取资源详情");
        ResourceDTO resourceDTO = resourceService.getResourceById(id);
        if (resourceDTO == null) {
            return Result.error("资源不存在");
        }
        ResourceVO resourceVO = MyBeanUtil.copyProperties(resourceDTO, ResourceVO::new);
        return Result.success(resourceVO);
    }

    @Override
    public Result<Long> insertResource(@RequestBody @NotNull ResourceVO resourceVO) {
        log.info("新增资源");
        ResourceDTO resourceDTO = MyBeanUtil.copyProperties(resourceVO, ResourceDTO::new);
        Long newResourceId = resourceService.insertResource(resourceDTO);
        if (newResourceId == null) {
            return Result.error("新增资源失败");
        }
        return Result.success(newResourceId);
    }

    @Override
    public Result<Boolean> updateResource(@RequestBody ResourceVO resourceVO) {
        log.info("更新资源");
        ResourceDTO resourceDTO = MyBeanUtil.copyProperties(resourceVO, ResourceDTO::new);
        Boolean result = resourceService.updateResource(resourceDTO);
        if (Boolean.FALSE.equals(result)) {
            return Result.error("更新资源失败");
        }
        return Result.success(result);
    }

    @Override
    public Result<Boolean> deleteResourceById(@RequestParam("id") @NotNull Long id) {
        log.info("删除资源");
        Boolean result = resourceService.deleteResourceById(id);
        if (Boolean.FALSE.equals(result)) {
            return Result.error("删除资源失败");
        }
        return Result.success(result);
    }

    @Override
    public Result<Boolean> deleteResourceByIdList(@RequestBody List<Long> idList) {
        log.info("批量删除资源");
        Boolean result = resourceService.deleteResourceByIdList(idList);
        if (Boolean.FALSE.equals(result)) {
            return Result.error("删除资源失败");
        }
        return Result.success(result);
    }

    @Override
    public Result<List<SubSystemTree>> getTreeResourceList() {
        log.info("获取资源树");
        List<SubSystemTree> subSystemTreeList = resourceService.getResourceTree();
        return Result.success(subSystemTreeList);
    }
}
